package com.atguigu.mapreduce.partitioner;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * KEYIN,reduce阶段输入的key的类型，Text
 * VALUEIN,reduce阶段输入的value类型，IntWriteable
 * KEYOUT,reduce阶段输出的key类型，Text
 * VALUEOUT,reduce阶段输出的value类型，IntWriteable
 * */
public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {

    private IntWritable outV = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {

        int sum = 0;

        // 循环数据累加-通过key循环
        for (IntWritable value : values) {
            sum += value.get();
        }

        outV.set(sum);
        // 写出
        context.write(key,outV);
    }
}
